package cn.xspace.wnx.db;

import com.xspace.android.xafcommon.been.BaseEntity;

import java.util.List;

import cn.xspace.chatui.bean.MsgRecordEntity;
import cn.xspace.chatui.data.IChatRecordData;
import rx.Observable;
import rx.Subscriber;

/**
 * Created by liuchen on 2016/10/14.
 */

public class ChatRecordDao extends BaseEntity {
    private static ChatRecordDao instance;
    public static final String TABLE_NAME = "t_chat_record";
    public static final String TABLE_NAME_TEMP = "t_chat_record_temp";

    public static ChatRecordDao getInstance() {
        if (instance == null) {
            instance = new ChatRecordDao();
        }
        return instance;
    }

    /**
     * 消息id
     */
    public static final String COLUMN_INSERTTIME = "inserttime";
    public static final String COLUMN_MSG_ID = "msg_id";
    public static final String COLUMN_MSG_SENDER_ID = "msg_sender_id";
    public static final String COLUMN_MSG_SENDER_NAME = "msg_sender_name";
    public static final String COLUMN_MSG_SENDER_LOGO = "msg_sender_logo";
    public static final String COLUMN_MSG_TEXT = "msg_text";
    public static final String COLUMN_MSG_CONTENT_TYPE = "msg_content_type";
    public static final String COLUMN_MSG_SEND_TIME = "msg_send_time";
    public static final String COLUMN_MSG_SEND_STATUS = "msg_send_status";
    public static final String COLUMN_MSG_TIP_TYPE = "tip_type";
    public static final String COLUMN_MSG_TYPE = "msg_type";
    public static final String COLUMN_MSG_GROUP_ID = "msg_group_id";
    public static final String COLUMN_MSG_GROUP_NAME = "msg_group_name";
    public static final String COLUMN_IS_SENDER_MINE = "is_sender_mine";

    public static final String COLUMN_FORM_ID = "form_id";
    public static final String COLUMN_FORM_URL = "form_url";
    public static final String COLUMN_FORM_TYPE = "form_type";
    public static final String COLUMN_FORM_STATUS = "form_status";
    public static final String COLUMN_MSG_UNREAD_FLAG = "msg_unread_flag";

    public static final String MSG_RECORD_TABLE_CREATE = "CREATE TABLE  IF NOT EXISTS "
            + ChatRecordDao.TABLE_NAME
            + " (_ID INTEGER PRIMARY KEY AUTOINCREMENT,"
            + ChatRecordDao.COLUMN_INSERTTIME + " TEXT,"
            + ChatRecordDao.COLUMN_MSG_ID + " TEXT,"
            + ChatRecordDao.COLUMN_MSG_SENDER_ID + " TEXT,"
            + ChatRecordDao.COLUMN_MSG_SENDER_NAME + " TEXT,"
            + ChatRecordDao.COLUMN_MSG_SENDER_LOGO + " TEXT,"
            + ChatRecordDao.COLUMN_MSG_TEXT + " TEXT,"
            + ChatRecordDao.COLUMN_MSG_CONTENT_TYPE + " INTEGER,"
            + ChatRecordDao.COLUMN_MSG_SEND_TIME + " IEXT,"
            + ChatRecordDao.COLUMN_MSG_SEND_STATUS + " INTEGER,"
            + ChatRecordDao.COLUMN_MSG_TIP_TYPE + " INTEGER,"
            + ChatRecordDao.COLUMN_MSG_TYPE + " INTEGER,"
            + ChatRecordDao.COLUMN_MSG_GROUP_ID + " TEXT,"
            + ChatRecordDao.COLUMN_MSG_GROUP_NAME + " TEXT,"
            + ChatRecordDao.COLUMN_IS_SENDER_MINE + " TEXT,"
            + ChatRecordDao.COLUMN_FORM_ID + " TEXT,"
            + ChatRecordDao.COLUMN_FORM_URL + " TEXT,"
            + ChatRecordDao.COLUMN_MSG_UNREAD_FLAG + " TEXT,"
            + ChatRecordDao.COLUMN_FORM_STATUS + " INTEGER,"
            + ChatRecordDao.COLUMN_FORM_TYPE + " INTEGER)";

    public static final String ADD_CLOUMN_TABLE_FORM_ID = "ALTER TABLE " + ChatRecordDao.TABLE_NAME + " ADD COLUMN " + ChatRecordDao.COLUMN_FORM_ID + " TEXT;";
    public static final String ADD_CLOUMN_TABLE_FORM_URL = "ALTER TABLE " + ChatRecordDao.TABLE_NAME + " ADD COLUMN " + ChatRecordDao.COLUMN_FORM_URL + " TEXT;";
    public static final String ADD_CLOUMN_TABLE_FORM_TYPE = "ALTER TABLE " + ChatRecordDao.TABLE_NAME + " ADD COLUMN " + ChatRecordDao.COLUMN_FORM_TYPE + " INTEGER;";

    public static final String RENAME_TO_TEMP_TABLE = "ALTER TABLE " + ChatRecordDao.TABLE_NAME + " RENAME TO " + ChatRecordDao.TABLE_NAME_TEMP;
    public static final String DROP_TEMP_TABLE = "DROP TABLE IF EXISTS " + ChatRecordDao.TABLE_NAME_TEMP;
    public static char str = '"';
    public static final String COPY_TEMP_TO_NEW_TABLE = "INSERT INTO " + ChatRecordDao.TABLE_NAME + " SELECT " +
            "_ID" + "," +
            COLUMN_INSERTTIME + "," +
            COLUMN_MSG_ID + "," +
            COLUMN_MSG_SENDER_ID + "," +
            COLUMN_MSG_SENDER_NAME + "," +
            COLUMN_MSG_SENDER_LOGO + "," +
            COLUMN_MSG_TEXT + "," +
            COLUMN_MSG_CONTENT_TYPE + "," +
            COLUMN_MSG_SEND_TIME + "," +
            COLUMN_MSG_SEND_STATUS + "," +
            COLUMN_MSG_TIP_TYPE + "," +
            COLUMN_MSG_TYPE + "," +
            COLUMN_MSG_GROUP_ID + "," +
            COLUMN_MSG_GROUP_NAME + "," +
            COLUMN_IS_SENDER_MINE + "," + str + str + "," + str + str + "," + str + str + "," + str + str + "," + str + str
            + "  FROM " + ChatRecordDao.TABLE_NAME_TEMP;


    private ChatRecordDao() {

    }

    IChatRecordData iChatRecordData = new IChatRecordData() {
        /**
         * 获得单聊群聊  记录列表
         */
        @Override
        public Observable<List<MsgRecordEntity>> getChatRecordList(final String id, final int chatType, final int pageIndex, final String formId) {

            return Observable.create(new Observable.OnSubscribe<List<MsgRecordEntity>>() {
                @Override
                public void call(Subscriber<? super List<MsgRecordEntity>> subscriber) {
                    List<MsgRecordEntity> list = WNXDBManager.getInstance().queryMsgRecordSingleAndGroup(id, chatType, pageIndex, formId);
                    subscriber.onNext(list);
                    subscriber.onCompleted();
                }
            });
        }

        /**
         * 插入聊天记录
         *
         * @param entity
         * @return
         */
        @Override
        public Observable<String> insertMsgRecord(final MsgRecordEntity entity) {
            return Observable.create(new Observable.OnSubscribe<String>() {
                @Override
                public void call(Subscriber<? super String> subscriber) {
                    WNXDBManager.getInstance().insertMsgRecord(entity);
                    subscriber.onNext("success");
                    subscriber.onCompleted();
                }
            });
        }

        /**
         * 更新聊天记录发送状态
         */
        @Override
        public Observable<String> updateMsgRecordSendStatus(final String inserttime, final int status) {
            return Observable.create(new Observable.OnSubscribe<String>() {
                @Override
                public void call(Subscriber<? super String> subscriber) {
                    WNXDBManager.getInstance().updateMsgSendStatus(inserttime, status);
                    subscriber.onCompleted();
                }
            });
        }

        /**
         * 更新聊天记录的消息id
         */
        @Override
        public Observable<String> updateMsgRecordMsgId(final String inserttime, final String msg_id) {
            return Observable.create(new Observable.OnSubscribe<String>() {
                @Override
                public void call(Subscriber<? super String> subscriber) {
                    WNXDBManager.getInstance().updateMsgSendMsgid(inserttime, msg_id);
                    subscriber.onCompleted();
                }
            });
        }

        @Override
        public Observable<Integer> deleteOneChatRecordByInserTime(final String inserttime) {
            return Observable.create(new Observable.OnSubscribe<Integer>() {
                @Override
                public void call(Subscriber<? super Integer> subscriber) {
                    subscriber.onNext(WNXDBManager.getInstance().deleteOneChatRecordByTime(inserttime));
                    subscriber.onCompleted();

                }
            });
        }

        @Override
        public Observable<Integer> deleteOneChatRecordByInserMsgId(final String msg_id) {
            return Observable.create(new Observable.OnSubscribe<Integer>() {
                @Override
                public void call(Subscriber<? super Integer> subscriber) {
                    subscriber.onNext(WNXDBManager.getInstance().deleteOneChatRecordById(msg_id));
                    subscriber.onCompleted();

                }
            });
        }

        @Override
        public Observable<String> updateLastTime(final String inserttime, final String lastTime) {
            return Observable.create(new Observable.OnSubscribe<String>() {
                @Override
                public void call(Subscriber<? super String> subscriber) {
                    WNXDBManager.getInstance().updateMsgSendTime(inserttime, lastTime);
                    subscriber.onCompleted();
                }
            });
        }

        @Override
        public Observable<String> updateChatRecordByTime(final String insertimte, final MsgRecordEntity entity) {
            return Observable.create(new Observable.OnSubscribe<String>() {
                @Override
                public void call(Subscriber<? super String> subscriber) {
                    WNXDBManager.getInstance().updateChatRecordByInserttime(insertimte, entity);
                    subscriber.onCompleted();
                }
            });
        }

        @Override
        public Observable<Integer> clearChatRecord(final String id, final int chatType) {
            return Observable.create(new Observable.OnSubscribe<Integer>() {
                @Override
                public void call(Subscriber<? super Integer> subscriber) {
//                    WNXDBManager.getInstance().updateChatRecordByInserttime(insertimte, entity);
                    int result = WNXDBManager.getInstance().deleteChatRecord(id, chatType);
                    subscriber.onNext(result);
                    subscriber.onCompleted();
                }
            });
        }

        @Override
        public Observable<String> clearChatRecordUnreadCount(final String id, final String form_id) {

            return Observable.create(new Observable.OnSubscribe<String>() {
                @Override
                public void call(Subscriber<? super String> subscriber) {
                    WNXDBManager.getInstance().clearChatRecordUnreadCount(id, form_id);
                    subscriber.onCompleted();
                }
            });
        }
    };


    public IChatRecordData getIChatRecordOperation() {
        return iChatRecordData;
    }

    /**
     * 删除单聊或者群聊记录
     *
     * @param id
     * @param chatType
     */
    public void deleteChatRecord(String id, int chatType) {
        WNXDBManager.getInstance().deleteChatRecord(id, chatType);
    }

}
